package net.m510.autosurfer.mhbk;

import java.io.File;
import java.net.URI;
import java.util.List;

import net.m510.autosurfer.common.FilePostProcess;
import net.m510.autosurfer.core.Parameters;

import org.apache.http.NameValuePair;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.FormElement;

public class MhbkDownload extends FilePostProcess {
	private MhbkCsvResponseHandler handler;
	
	public MhbkDownload() {
		this(null,null,null);
	}
	
	public MhbkDownload(CloseableHttpClient client, HttpClientContext context, File file) {
		super(client, context, file);
		this.handler = new MhbkCsvResponseHandler(file);
	}

	@Override
	protected URI getUri(Document doc) {
		URI uri = URI.create(context.getTargetHost().toURI());
		return uri.resolve("/servlet/ACCHST0400001B.do");
	}

	@Override
	protected List<NameValuePair> getRequestParameters(Document doc) {
		FormElement form = doc.getAllElements().forms().get(0);
		Parameters p = Parameters.create(form);
		p.put("rdoInqMtdSpec", "2");
		return p.asNameValueList();
	}
	@Override
	protected ResponseHandler<File> getResponseHandler() {
		return handler;
	}
	
	public Document getOutputDocuemnt() {
		return handler.getDocument();
	}
}
